<template>
	<view class="page-ranking" :style="'background-image: url('+ config.index_bc + ')'">
		<view class="r-tips">
			{{listType === 1 ? '按战队每日平均步数排名' : '按战队累计平均步数排名'}}
		</view>
		<view class="search-box">
			<view style="flex: 1;">
				<u-search v-model="keyWords" :show-action="false" height="62" color="#fff" search-icon-color="#aaa" bg-color="#06B2FF"
					border-color="#fff" disabled placeholder="请输入战队名称" placeholder-color="#fff" @click="showPopup=true"></u-search>
			</view>
		</view>
		<view class="current-box" v-if="currentData.rowNo !== 0">
			<view class="number">
				{{currentData.rowNo}}
			</view>
			<view class="item-name">
				{{currentData.name}}
			</view>
			<view class="steps">
				{{parseInt(currentData.step || 0)}}
			</view>
			<view class="like" @click="tapLike(currentData)">
				<view class="count" :class="currentData.isLike ? 'red' : ''">
					{{currentData.like}}
				</view>
				<view class="icon-box">
					<image class="icon" :src="currentData.isLike ? config.like1 : config.like0"
						mode="scaleToFill"></image>
				</view>
			</view>
		</view>
		<view class="btn-box">
			<view class="btn" :class="listType === 1 ? 'current' : ''" @click="listTypeChange(1)">
				今日战斗排名
			</view>
			<view class="btn" :class="listType === 2 ? 'current' : ''" @click="listTypeChange(2)">
				累计战斗总排名
			</view>
		</view>
		<view class="list-box">
			<scroll-view scroll-y="true" class="scroll-box" @scrolltolower="loadMore" refresher-enabled	:refresher-triggered="triggered" @refresherpulling="onPulling"
            @refresherrefresh="onRefresh" @refresherrestore="onRestore">
				<template v-for="(item, index) in list">
					<view class="top-three-item" :key="index" v-if="index < 3" @click="toPage('/pages/ranking/ranking?cmId='+item.cmId)">
						<view class="top-number" :style="'background-image: url('+ config[topIconList[index]] + ')'">
							{{index + 1}}
						</view>
						<view class="number"></view>
						<view class="item-name">
							{{item.company}}
						</view>
						<view class="steps">
							{{parseInt(item.step || 0)}}
						</view>
						<view class="like" @click.stop="tapLike(item)">
							<view class="count" :class="item.isLike ? 'red' : ''">
								{{item.like}}
							</view>
							<view class="icon-box">
								<image class="icon" :src="item.isLike ? config.like1 : config.like0" mode="scaleToFill"></image>
							</view>
						</view>
					</view>
					<view class="item" v-else :key="index" @click="toPage('/pages/ranking/ranking?cmId='+item.cmId)">
						<view class="item-number">
							{{index + 1}}
						</view>
						<view class="item-name">
							{{item.company}}
						</view>
						<view class="steps">
							{{parseInt(item.step || 0)}}
						</view>
						<view class="like" @click.stop="tapLike(item)">
							<view class="count" :class="item.isLike ? 'red' : ''">
								{{item.like}}
							</view>
							<view class="icon-box">
								<image class="icon" :src="item.isLike ? config.like1 : config.like0" mode="scaleToFill"></image>
							</view>
						</view>
					</view>
				</template>
				<view class="load-more-box" style="margin-top: 40rpx;" v-show="!keyWords">
					<u-loadmore :status="status" />
				</view>
			</scroll-view>
		</view>
		
		<u-popup v-model="showPopup" mode="right" width="100%" height="100%">
			<view class="p-search-box">
				<u-search v-model="keyWords" placeholder="请输入战队名称" action-text="取消" @change="search"
					@custom="keyWords='',showPopup=false"></u-search>
				<scroll-view scroll-y="true" class="list-box">
					<view class="item" :style="{borderColor: config.index_border_color}" v-for="(item, index) in searchList"
						:key="index" @click="select(item)">
						<view class="number-box">
							<view class="name">
								{{item.company}}
							</view>
						</view>
						<view class="step-box">
							<!-- <view class="step" :style="{backgroundColor: config.index_border_color}">
								{{numberFormat(item.)}}步
							</view> -->
							<view class="icon">
								<image class="img" :src="config.list_item_right_icon" mode="scaleToFill"></image>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				config: {
					index_bc: '',
					index_border_color: '',
					like0: '',
					like1: '',
					top_one_icon: '',
					top_two_icon: '',
					top_three_icon: '',
					list_item_right_icon: ''
				},
				currentData: {
					cmId: '',
					rowNo: 0,
					name: '',
					step: 0,
					like: 0,
					isLike: false
				},
				topIconList: ['top_one_icon', 'top_two_icon', 'top_three_icon'],
				listType: 1,
				list: [],
				status: 'loadmore',
				pageIndex: 1,
				triggered: false,
				showPopup: false,
				keyWords: '',
				cmId: '',
				cmName: '',
				searchList: []
			}
		},
		onLoad() {
			this.getList()
			this._freshing = false;
			this.search('')
		},
		watch: {
			keyWords(value) {
				if(value === '') {
					this.pageIndex = 1
					this.getList()
				}
			}
		},
		methods: {
			listTypeChange(type) {
				this.listType = type;
				this.pageIndex = 1;
				this.getList()
			},
			async getList() {
				const [res, err] = await this.$get('xcx/findCompanyRange', {
					cmId: this.userInfo.cmId,
					uid: this.userInfo.uid,
					pageIndex: this.pageIndex,
					pageSize: 20,
					tag: this.listType
				});
				if(err) return
				const range = res.data.range
				this.currentData.rowNo = range.rowNo;
				this.currentData.name = range.company;
				this.currentData.step = range.step;
				this.currentData.cmId = range.cm_id
				this.currentData.like = range.like;
				this.currentData.isLike = range.isLike;
				this.list = res.data.list;
				if(res.data.list.length < 20) this.status = 'nomore'
				else this.status = 'loadmore'
			},
			async loadMore() {
				if(this.status === 'nomore') return
				this.pageIndex ++;
				this.status = 'loading'
				const [res, err] = await this.$get('xcx/findCompanyRange', {
					cmId: this.userInfo.cmId,
					uid: this.userInfo.uid,
					pageIndex: this.pageIndex,
					pageSize: 10,
					tag: this.listType
				});
				if(err) return
				this.list = [...this.list, ...res.data.list];
				if(res.data.list.length < 10) this.status = 'nomore'
				else this.status = 'loadmore'
			},
			onPulling(e) {
				this.triggered = true;
			},
			async onRefresh() {
				if (this._freshing) return;
				this._freshing = true;
				this.pageIndex = 1;
				await this.getList()
				this.triggered = false;
				this._freshing = false;
			},
			onRestore() {
				this.triggered = 'restore'; // 需要重置
			},
			async tapLike(item) {
				const reqData = {
					uid: this.userInfo.uid,
					likeId: item.cmId,
					tag: 2,
					cancelTag: item.isLike ? 2 : 1
				}
				const [res, err] = await this.$post('xcx/pointLike', reqData);
				if(err) {
					uni.showToast({
						title: '点赞/取消点赞失败',
						icon: 'none'
					})
					return
				}
				item.isLike = !item.isLike;
				if(item.isLike) item.like = item.like + 1
				else item.like = item.like - 1
			},
			search(keyWords) {
				this.$u.debounce(async () => {
					const [res, err] = await this.$get('xcx/findCompanyList?name=' + keyWords);
					if(err) return
					if(res.data.length < 1) {
						uni.showToast({
							title: '暂无数据',
							icon: 'none'
						})
					}
					this.searchList = res.data
				}, 500)
			},
			async select(item) {
				this.cmId = item.cmId;
				this.cmName = item.company;
				this.showPopup = false;
				const [res, err] = await this.$get('xcx/findCompanyRange', {
					cmId: this.cmId,
					uid: this.userInfo.uid,
					pageIndex: this.pageIndex,
					pageSize: 20,
					tag: this.listType
				});
				if(err) return
				const range = res.data.range
				this.currentData.rowNo = range.rowNo;
				this.currentData.name = range.company;
				this.currentData.steps = range.step;
				this.currentData.cmId = range.cm_id
				this.currentData.like = range.like;
				this.currentData.isLike = range.isLike;
				this.list = [];
			},
			toPage(url) {
				uni.navigateTo({
					url
				})
			},
		}
	}
</script>

<style lang="scss">
	.page-ranking {
		width: 100%;
		height: 100%;
		background-size: 160% auto;
		background-repeat: no-repeat;
		background-position: -160rpx 10rpx;
		
		.r-tips {
			height: 80rpx;
			background-color: #06B2FF;
			color: #FFFFFF;
			text-align: center;
			line-height: 80rpx;
			font-size: 24rpx;
		}

		.search-box {
			width: 100%;
			height: 126rpx;
			padding: 0rpx 38rpx 32rpx 38rpx;
			display: flex;
			background-color: #06B2FF;
		}
		
		.btn-box {
			width: 100%;
			height: 124rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;
			background-color: rgba(255, 255, 255, .5);
			
			.btn {
				width: 244rpx;
				height: 76rpx;
				// background: #E5404F;
				background-color: #06B2FF;
				border-radius: 100rpx;
				line-height: 76rpx;
				text-align: center;
				color: #FFFFFF;
				font-size: 28rpx;
				
				&.current {
					background-color: #E5404F;
				}
			}
		}

		.list-box {
			width: 100%;
			height: calc(100% - 126rpx - 124rpx - 124rpx - 80rpx);
			background-color: rgba(255, 255, 255, .5);

			.scroll-box {
				width: 100%;
				height: 100%;
				box-sizing: border-box;
				// padding-top: 124rpx;
				// position: relative;
				// z-index: 10;
			}
		}
		
		// .current-box {
		// 	position: absolute;
		// 	top: -124rpx;
		// 	left: 0;
		// 	z-index: 100;
		// }
		
		.current-box {
			background-color: rgba(255, 255, 255, .5);
		}

		.current-box,
		.top-three-item,
		.item {
			width: 100%;
			height: 124rpx;
			opacity: 1;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 30rpx;
			box-sizing: border-box;
			border-bottom: 2rpx solid #E8EAED;

			.number {
				width: 40rpx;
				font-size: 28rpx;
				color: #333;
			}

			.item-number {
				width: 40rpx;
				font-size: 28rpx;
				color: #C4CAD4;
			}

			.head-box {
				width: 88rpx;
				height: 88rpx;
				background: rgba(0, 0, 0, 0);
				border-style: solid;
				border-width: 4rpx;
				border-radius: 50%;
				margin-left: 30rpx;
			}

			.name {
				font-size: 28rpx;
				color: #FFFFFF;
				margin-left: 30rpx;
				flex: 1;
			}

			.item-name {
				color: #333333;
				font-size: 28rpx;
				margin-left: 30rpx;
				flex: 1;
			}

			.steps {
				font-size: 36rpx;
				color: #333;
			}

			.like {
				margin-left: 30rpx;

				.count {
					font-size: 28rpx;
					text-align: center;
					color: #C4CAD4;

				}

				.count.light {
					color: #FFFFFF;
				}
				
				.red {
					color: #E5404F !important;
				}

				.icon-box {
					.icon {
						width: 48rpx;
						height: 48rpx;
					}
				}
			}
		}

		.top-three-item {
			position: relative;

			.top-number {
				width: 80rpx;
				height: 90rpx;
				position: absolute;
				left: 0rpx;
				top: 0rpx;
				background-repeat: no-repeat;
				background-size: 100% auto;
				color: #FFFFFF;
				font-size: 28rpx;
				padding: 16rpx;
			}
		}
		
		.p-search-box {
			width: 100%;
			height: 100%;
			padding: 20rpx 34rpx;
			box-sizing: border-box;
		
			.list-box {
				width: 100%;
				margin-top: 40rpx;
				height: calc(100% - 64rpx - 40rpx);
		
				.item {
					width: 672rpx;
					height: 98rpx;
					border: 2rpx solid #2A83CA;
					border-radius: 20rpx;
					margin: 0 auto 20rpx auto;
					padding: 20rpx 20rpx 20rpx 26rpx;
					box-sizing: border-box;
					display: flex;
					align-items: center;
					justify-content: space-between;
		
					.number-box {
						display: flex;
						align-items: center;
		
						.number {
							width: 78rpx;
							height: 58rpx;
							background: #2A83CA;
							opacity: 1;
							font-size: 40rpx;
							line-height: 58rpx;
							color: #FFFFFF;
							text-align: center;
							border-radius: 10rpx;
						}
		
						.name {
							font-size: 28rpx;
							line-height: 48rpx;
							color: #333333;
							text-shadow: 0px 2rpx 4rpx rgba(0, 0, 0, 0.16);
							margin-left: 18rpx;
						}
					}
		
					.step-box {
						display: flex;
						align-items: center;
		
						.step {
							width: 140rpx;
							height: 46rpx;
							background: #2A83CA;
							opacity: 1;
							border-radius: 10rpx;
							font-size: 20rpx;
							line-height: 34rpx;
							color: #FFFFFF;
							line-height: 46rpx;
							text-align: center;
						}
		
						.icon {
							width: 23rpx;
							height: 29rpx;
							margin-left: 20rpx;
		
							.img {
								width: 23rpx;
								height: 29rpx;
							}
						}
					}
				}
			}
		}
	}
</style>
